<!DOCTYPE html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="一.定义范型1class A&amp;lt;T&amp;gt; &amp;#123;&amp;#125; (1)范型可以使用有界表示法extends 表示下界surper 表示上界class B {} # 表示只有A或者A的子类可接受class C{} # 表示只有F或者是F的父类可接受  注意 ：范型中的T不能进行强制转化—静态编译型语言特点  (2)范型通配符 ?1234567891011121314151617181920">
<meta property="og:type" content="article">
<meta property="og:title" content="kyssion-blog">
<meta property="og:url" content="http://yoursite.com/2018/10/09/Java-范型/index.html">
<meta property="og:site_name" content="kyssion-blog">
<meta property="og:description" content="一.定义范型1class A&amp;lt;T&amp;gt; &amp;#123;&amp;#125; (1)范型可以使用有界表示法extends 表示下界surper 表示上界class B {} # 表示只有A或者A的子类可接受class C{} # 表示只有F或者是F的父类可接受  注意 ：范型中的T不能进行强制转化—静态编译型语言特点  (2)范型通配符 ?1234567891011121314151617181920">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-10-09T07:22:37.257Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="kyssion-blog">
<meta name="twitter:description" content="一.定义范型1class A&amp;lt;T&amp;gt; &amp;#123;&amp;#125; (1)范型可以使用有界表示法extends 表示下界surper 表示上界class B {} # 表示只有A或者A的子类可接受class C{} # 表示只有F或者是F的父类可接受  注意 ：范型中的T不能进行强制转化—静态编译型语言特点  (2)范型通配符 ?1234567891011121314151617181920">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Mist',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2018/10/09/Java-范型/"/>





  <title> | kyssion-blog</title>
  








</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">kyssion-blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle"></p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/sitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/10/09/Java-范型/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="kyssion">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="kyssion-blog">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline"></h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2018-10-09T15:22:37+08:00">
                2018-10-09
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="一-定义范型"><a href="#一-定义范型" class="headerlink" title="一.定义范型"></a>一.定义范型</h3><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">A</span>&lt;<span class="title">T</span>&gt; </span>&#123;&#125;</span><br></pre></td></tr></table></figure>
<h4 id="1-范型可以使用有界表示法"><a href="#1-范型可以使用有界表示法" class="headerlink" title="(1)范型可以使用有界表示法"></a>(1)范型可以使用有界表示法</h4><p><strong>extends</strong> 表示下界<br><strong>surper</strong> 表示上界<br><strong>class B <t extends="" a="">{}</t></strong> # 表示只有A或者A的子类可接受<br><strong>class C<t super="" f="">{}</t></strong> # 表示只有F或者是F的父类可接受</p>
<blockquote>
<p>注意 ：范型中的T不能进行强制转化—静态编译型语言特点</p>
</blockquote>
<h4 id="2-范型通配符"><a href="#2-范型通配符" class="headerlink" title="(2)范型通配符 ?"></a>(2)范型通配符 ?</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//点是集合类型使用通配符的时候见下面的例子</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Other</span></span>&#123;</span><br><span class="line">	<span class="comment">//?通配符表示一般的匹配相关的有嗯来约束一类的类型</span></span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">Rong</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">One</span></span>&#123;&#125;</span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">Two</span> <span class="keyword">extends</span> <span class="title">One</span></span>&#123;&#125;</span><br><span class="line">	<span class="class"><span class="keyword">class</span> <span class="title">Three</span> <span class="keyword">extends</span> <span class="title">Two</span></span>&#123;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">methodone</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		Rong&lt;? extends Two&gt; rong = <span class="keyword">new</span> Rong&lt;Three&gt;();</span><br><span class="line">		<span class="comment">//Rong&lt;? extends Two&gt; rong2 = new Rong&lt;One&gt;();//报错了因为不满足上界</span></span><br><span class="line">		Rong&lt; ? <span class="keyword">super</span> Two&gt; rong3 = <span class="keyword">new</span> Rong&lt;One&gt;();</span><br><span class="line">		<span class="comment">//---注意--- 集合类型的通配符模式下 	extends只能取不能存取得是extends后面的类型  </span></span><br><span class="line">		<span class="comment">//								surper 只能存变量强制转化成Object类型不能取</span></span><br><span class="line">		List&lt;? extends Two&gt; list=<span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">		One one= list.get(<span class="number">0</span>);</span><br><span class="line">		Two two = list.get(<span class="number">0</span>);<span class="comment">//因为extends表示存的类型都是Two的子类但是添加的时候只能加入一种类型，所以只能取不能存 super反之</span></span><br><span class="line">		<span class="comment">//list.set(0, new Three());//报错</span></span><br><span class="line">		List&lt;? <span class="keyword">super</span> Two&gt; list2 = <span class="keyword">new</span> ArrayList&lt;&gt;();</span><br><span class="line">		list2.add(<span class="keyword">new</span> Three());<span class="comment">//适因为内部一定知道相关 术语上转型</span></span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="三-继承中的范型"><a href="#三-继承中的范型" class="headerlink" title="三.继承中的范型"></a>三.继承中的范型</h3><blockquote>
<p>只要保证外部的范型类型必须实现内部的</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">ThisisT</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Father</span>,<span class="title">V</span>&gt;</span>&#123;</span><br><span class="line">	<span class="comment">//注意泛型不能是基本类型只能是引用类型  但是java 的自定装箱机制会将基本类型变成引用类型</span></span><br><span class="line">	<span class="comment">//因为引用类型 不能是基本类型 意味着 声明 泛型的时候不能使用引用类型</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ThisisT</span><span class="params">(T heh)</span> </span>&#123;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ThisisT</span><span class="params">(V v)</span></span>&#123;&#125;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">ThisisT</span><span class="params">(T t,V v)</span></span>&#123;</span><br><span class="line">		<span class="comment">//</span></span><br><span class="line">		t.thisisFatherMehthod();</span><br><span class="line">	&#125;</span><br><span class="line">	<span class="comment">//? 通配符  其实和T所带表的意义是是相同的 但是 ? 通配符是为了解决   函数 内部的方法调用 泛型类的时候所采用的因为泛型类中不能使用除了已经声明的泛型之外的字母</span></span><br><span class="line">	<span class="comment">//其余的 T等是一样的方法</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">method</span><span class="params">(ArrayList&lt;?&gt; arrayList)</span></span>&#123;<span class="comment">//这样这个方法就能接受除了T V 类型之外的其他类型</span></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Father</span></span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">thisisFatherMehthod</span><span class="params">()</span></span>&#123;&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">interface</span> <span class="title">Mother</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">ArrayList</span>&lt;<span class="title">T</span>&gt;&gt;</span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ThisisMatherMethod</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//--继承或者 实现的接口 终不能写extends等语句  具体的实现需要在新的类名称中写 官方属于叫做声明值的传递</span></span><br><span class="line"><span class="comment">//继承泛型类  ---</span></span><br><span class="line"><span class="class"><span class="keyword">class</span>  <span class="title">Ceshi</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">ArrayList</span>&lt;<span class="title">T</span>&gt;&gt; <span class="keyword">implements</span> <span class="title">Mother</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line"> </span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ThisisMatherMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//继承泛型接口</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ceshi2</span>&lt;<span class="title">T</span> <span class="keyword">extends</span> <span class="title">Father</span>, <span class="title">V</span>&gt; <span class="keyword">extends</span> <span class="title">ThisisT</span>&lt;<span class="title">T</span>,<span class="title">V</span>&gt;</span>&#123;</span><br><span class="line"> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Ceshi2</span><span class="params">(T heh)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>(heh);</span><br><span class="line">		<span class="comment">// TODO Auto-generated constructor stub</span></span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">//继承泛型类外加泛型接口</span></span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Ceshi3</span>&lt;<span class="title">M</span> <span class="keyword">extends</span> <span class="title">ArrayList</span>&lt;<span class="title">M</span>&gt;, <span class="title">V</span>, <span class="title">T</span> <span class="keyword">extends</span> <span class="title">Father</span>&gt; <span class="keyword">extends</span> <span class="title">ThisisT</span>&lt;<span class="title">T</span>,<span class="title">V</span>&gt; <span class="keyword">implements</span> <span class="title">Mother</span>&lt;<span class="title">M</span>&gt;</span>&#123;</span><br><span class="line"> </span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="title">Ceshi3</span><span class="params">(T heh)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">super</span>(heh);</span><br><span class="line">		<span class="comment">// TODO Auto-generated constructor stub</span></span><br><span class="line">	&#125;</span><br><span class="line"> </span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">ThisisMatherMethod</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">BBB</span>&lt;<span class="title">T</span>&gt;</span>&#123;</span><br><span class="line">	<span class="function"><span class="keyword">public</span> T <span class="title">say</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">AAA</span> <span class="keyword">extends</span> <span class="title">BBB</span>&lt;<span class="title">Integer</span>&gt;</span>&#123;<span class="comment">//范型继承可以是已知的类型使用使用已知了型可以在前面不加上属性，但是如果前面加上属性将导致后面的类型变成未知</span></span><br><span class="line">	<span class="meta">@Override</span></span><br><span class="line">	<span class="function"><span class="keyword">public</span> Integer <span class="title">say</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="comment">// TODO Auto-generated method stub</span></span><br><span class="line">		<span class="keyword">return</span> <span class="number">123</span>;</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="四-范型的底层实现"><a href="#四-范型的底层实现" class="headerlink" title="四.范型的底层实现"></a>四.范型的底层实现</h3><p>为了实现与非泛型代码的兼容，Java语言的泛型采用<strong>擦除(Erasure)</strong>来实现，也就是泛型基本上由编译器来实现，由编译器执行类型检查和类型推断，然后在生成字节码之前将其清<strong>除掉</strong>，虚拟机是不知道泛型存在的。这样的话，泛型和非泛型的代码就可以混合运行，当然了，也显得相当混乱。</p>
<p>在使用泛型时，会有一个对应的类型叫做原生类型(raw<br>type)，泛型类型会被擦除到原生类型，如Generic<t>会被查处到Generic，<strong>List<string></string></strong>会被查处到List，由于擦除，在虚拟机中无法获得任何类型信息，虚拟机只知道原生类型。下面的代码将展示Java泛型的真相–擦除：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Erasure</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">	<span class="keyword">private</span> T t;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(T t)</span> </span>&#123;</span><br><span class="line">		<span class="keyword">this</span>.t = t;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> T <span class="title">get</span><span class="params">()</span> </span>&#123;</span><br><span class="line">		<span class="keyword">return</span> t;</span><br><span class="line">	&#125;</span><br><span class="line">	</span><br><span class="line">	<span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;	</span><br><span class="line">		Erasure&lt;String&gt; eras = <span class="keyword">new</span> Erasure&lt;String&gt;();</span><br><span class="line">		eras.set(<span class="string">"not real class type"</span>);</span><br><span class="line">		String value = eras.get();</span><br><span class="line">		</span><br><span class="line">	&#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></t></p>
<p>使用javap反编译class文件，得到如下代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">com</span>.<span class="title">think</span>.<span class="title">generics</span>.<span class="title">Erasure</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">  com.think.generics.Erasure();</span><br><span class="line">    Code:</span><br><span class="line">       <span class="number">0</span>: aload_0       </span><br><span class="line">       1: invokespecial #12                 // Method java/lang/Object."&lt;init&gt;":()V</span><br><span class="line">       <span class="number">4</span>: <span class="keyword">return</span>        </span><br><span class="line"> </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">set</span><span class="params">(T)</span></span>;</span><br><span class="line">    Code:</span><br><span class="line">       <span class="number">0</span>: aload_0       </span><br><span class="line">       <span class="number">1</span>: aload_1       </span><br><span class="line">       2: putfield      #23                 // Field t:Ljava/lang/Object;</span><br><span class="line">       <span class="number">5</span>: <span class="keyword">return</span>        </span><br><span class="line"> </span><br><span class="line">  <span class="function"><span class="keyword">public</span> T <span class="title">get</span><span class="params">()</span></span>;</span><br><span class="line">    Code:</span><br><span class="line">       <span class="number">0</span>: aload_0       </span><br><span class="line">       1: getfield      #23                 // Field t:Ljava/lang/Object;</span><br><span class="line">       <span class="number">4</span>: areturn       </span><br><span class="line"> </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(java.lang.String[])</span></span>;</span><br><span class="line">    Code:</span><br><span class="line">       0: new           #1                  // class com/think/generics/Erasure</span><br><span class="line">       <span class="number">3</span>: dup           </span><br><span class="line">       4: invokespecial #30                 // Method "&lt;init&gt;":()V</span><br><span class="line">       <span class="number">7</span>: astore_1      </span><br><span class="line">       <span class="number">8</span>: aload_1       </span><br><span class="line">       9: ldc           #31                 // String not real class type</span><br><span class="line">      11: invokevirtual #33                 // Method set:(Ljava/lang/Object;)V</span><br><span class="line">      <span class="number">14</span>: aload_1       </span><br><span class="line">      15: invokevirtual #35                 // Method get:()Ljava/lang/Object;</span><br><span class="line">      18: checkcast     #37                 // class java/lang/String</span><br><span class="line">      <span class="number">21</span>: astore_2      </span><br><span class="line">      <span class="number">22</span>: <span class="keyword">return</span>        </span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>从反编译出来的字节码可以看到，泛型Erasure<t>被擦除到了Erasure，其内部的字段T被擦除到了Object，可以看到get和set方法中都是把t作为Object来使用的。最值得关注的是，反编译代码的倒数第三行，对应到Java代码就是String<br>value =<br>eras.get();编译器执行了类型转换。这就是Java泛型的本质：对传递进来的值进行额外的编译期检查，并插入对传递出去的值的转型。这样的泛型真的是泛型吗？</t></p>
<p>即便我们可以说，Java中的泛型确实不是真正的泛型，但是它带来的好处还是显而易见的，它使得Java的类型安全前进了一大步，原本需要程序员显式控制的类型转换，现在改由编译器来实现，只要你按照泛型的规范去编写代码，总会得到安全的保障。在这里，我们不得不思考一个问题，理解Java泛型，那么其核心目的是什么？我个人认为，Java泛型的核心目的在于安全性，尤其是在理解泛型通配符时，一切奇怪的规则，归根结底都是处于安全的目的。</p>
<p><strong>类型信息的丢失</strong></p>
<p>由于擦除的原因，在泛型代码内部，无法获得任何有关泛型参数类型的信息。在运行时，虚拟机无法获得确切的类型信息，一切以来确切类型信息的工作都无法完成，比如instanceof操作，和new表达式，<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><span class="line"><span class="class"><span class="keyword">class</span>  <span class="title">Erasure</span>&lt;<span class="title">T</span>&gt;  </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">f</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(arg <span class="keyword">instanceof</span> T) <span class="comment">//Error</span></span><br><span class="line">        T ins = <span class="keyword">new</span> T();<span class="comment">//Error</span></span><br><span class="line">        T[] array = <span class="keyword">new</span> T[<span class="number">10</span>];<span class="comment">//error</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line">那么在需要具体的类型信息时，我们就要记住Class对象来实现了，凡是在运行时需要类型信息的地方，都使用Class对象来进行操作，比如：</span><br><span class="line"><span class="class"><span class="keyword">class</span> <span class="title">Erasure</span>&lt;<span class="title">T</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> Class&lt;T&gt; clazz;</span><br><span class="line">    Erasure(Class&lt;T&gt; kind) &#123;</span><br><span class="line">        clazz = kind;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">f</span><span class="params">()</span> </span>&#123;</span><br><span class="line">        <span class="keyword">if</span>(clazz.isInstance(arg)) &#123;&#125;</span><br><span class="line">        T t = clazz.newInstance();<span class="comment">//必须要有无参构造方法</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<h3 id="五-型的相关坑点"><a href="#五-型的相关坑点" class="headerlink" title="五.型的相关坑点"></a>五.型的相关坑点</h3><ul>
<li><strong>范型不是类型绑定的所以 List<a> 和 List<b> 不是同一个类可以使用通配符将两个属性变成相同的</b></a></strong></li>
<li><strong>泛型类中的数组对数组的限制 不能实例化泛型数组 其次不能声明 泛型组成的数组</strong></li>
<li><strong>不能实例化范型 new T();</strong></li>
</ul>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/10/09/README/" rel="next" title="">
                <i class="fa fa-chevron-left"></i> 
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/10/09/Serverless（baas & faas）无服务器计算/" rel="prev" title="">
                 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">kyssion</p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/">
              
                  <span class="site-state-item-count">133</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                
                  <span class="site-state-item-count">3</span>
                  <span class="site-state-item-name">分类</span>
                
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                
                  <span class="site-state-item-count">2</span>
                  <span class="site-state-item-name">标签</span>
                
              </div>
            

          </nav>

          

          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一-定义范型"><span class="nav-number">1.</span> <span class="nav-text">一.定义范型</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#1-范型可以使用有界表示法"><span class="nav-number">1.1.</span> <span class="nav-text">(1)范型可以使用有界表示法</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-范型通配符"><span class="nav-number">1.2.</span> <span class="nav-text">(2)范型通配符 ?</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三-继承中的范型"><span class="nav-number">2.</span> <span class="nav-text">三.继承中的范型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四-范型的底层实现"><span class="nav-number">3.</span> <span class="nav-text">四.范型的底层实现</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#五-型的相关坑点"><span class="nav-number">4.</span> <span class="nav-text">五.型的相关坑点</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">kyssion</span>

  
</div>


  <div class="powered-by">由 <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> 强力驱动</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">主题 &mdash; <a class="theme-link" target="_blank" href="https://github.com/iissnan/hexo-theme-next">NexT.Mist</a> v5.1.4</div>




        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

</body>
</html>
